package procsim;

public class DFlipFlop extends FlipFlop{
    private Signal inSig, inSigPrev;
    public DFlipFlop(Signal in){
        super();
        if(in!=null) in.addElement(this);
        inSig=in;
        inSigPrev=new Signal(0);
    }
    public DFlipFlop(){
        super();
        inSigPrev=new Signal(0);
    }
    public DFlipFlop setDSignal(Signal dsig){
        if(dsig!=null) dsig.addElement(this);
        inSig=dsig;
        return this;
    }
    @Override
    public DFlipFlop tick(){
        outSig.set(outSigPrev);
        if(outSigResult!=null) outSigResult.set(outSig);
        outSigPrev.set(inSig);
        calculateComplement();
        inSigPrev.set(inSig);
        return this;
    }
    public DFlipFlop ammend(Signal Sig){
        if(Sig.get()==inSigPrev.get()) return this;
        outSigPrev.set(Sig.get());
        return this;
    }
    @Override
    public void updateSignal(Sig sig){
        if(ticking) return;
        if(inSig!=null && inSig==sig)
            inSig.set(sig.get(), false);
    }
}
